//
//  NXAPickerView.m
//  NXA-MasterDrivingSkills
//
//  Created by jps on 2017/1/6.
//  Copyright © 2017年 NXA. All rights reserved.
//

#import "NXAPickerView.h"

@interface NXAPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,weak)UIPickerView *pickerView;
@property(nonatomic,strong)UIButton *confirmBtn;  //!<确定按钮

@property(nonatomic,strong)NSMutableArray *valueArrM;

@property(nonatomic,copy)NSString *firstString;
@property(nonatomic,copy)NSString *secondString;
@property(nonatomic,copy)NSString *thirdString;

@end


@implementation NXAPickerView


- (void)layoutSubviews
{
    self.firstDataSource = @[@"A1", @"A2", @"A3"];
    self.secondDataSource = @[@"科目一", @"科目二", @"科目三"];
    self.thirdDataSource = @[@"0", @"1", @"2"];
    
    [self initComponents];
}


- (void)initComponents
{
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    
    UIView *v = [[UIView alloc]initWithFrame:CGRectZero];
    v.layer.cornerRadius = 8;
    v.layer.masksToBounds = YES;
    v.center = self.center;
    v.bounds = CGRectMake(0, 0, CURRENT_SCREEN_WIDTH-80, 280);
    v.backgroundColor = [UIColor whiteColor];
    [self addSubview:v];
    
    CGFloat w = v.frame.size.width/3;
    UILabel *carTypeLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, w, 40)];
    carTypeLable.text = @"培训车型";
    carTypeLable.adjustsFontSizeToFitWidth = YES;
    carTypeLable.textColor = [UIColor lightGrayColor];
    carTypeLable.textAlignment = NSTextAlignmentCenter;
    [v addSubview:carTypeLable];
    UILabel *kemuLabel = [[UILabel alloc]initWithFrame:CGRectMake(w, 0, w, 40)];
    kemuLabel.text = @"培训科目";
    kemuLabel.adjustsFontSizeToFitWidth = YES;
    kemuLabel.textColor = [UIColor lightGrayColor];
    kemuLabel.textAlignment = NSTextAlignmentCenter;
    [v addSubview:kemuLabel];
    UILabel *countLabel = [[UILabel alloc]initWithFrame:CGRectMake(w*2, 0, w, 40)];
    countLabel.text = @"培训人数";
    countLabel.adjustsFontSizeToFitWidth = YES;
    countLabel.textAlignment = NSTextAlignmentCenter;
    countLabel.textColor = [UIColor lightGrayColor];
    [v addSubview:countLabel];
    
    [v addSubview:self.pickerView];
    
    self.confirmBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.pickerView.frame), v.frame.size.width, 40)];
    [self.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [self.confirmBtn setBackgroundColor:ThemeColor];
    [self.confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [v addSubview:self.confirmBtn];
}




#pragma mark - handleEvents

- (void)confirmBtnClick:(UIButton *)sender
{
    if (_transValueCallBack) {
        _transValueCallBack(self.valueArrM);
    }
    
    [self removeFromSuperview];
}

#pragma mark UIPickerView DataSource

//返回列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return self.column;
}

//返回每列里边的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    if (component == 0) {
        return self.firstDataSource.count;
        
    }else if (component == 1){
        return self.secondDataSource.count;
        
    }else {
        return self.thirdDataSource.count;
    }
}




#pragma mark --- UIPickerViewDelegate代理方法


//指定每行如何展示数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return self.firstDataSource[row];
        
    }else if (component == 1){
        return self.secondDataSource[row];
        
    }else{
        return self.thirdDataSource[row];
    }
    
}

//选择器选择的方法  row：被选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        [self.valueArrM replaceObjectAtIndex:0 withObject:self.firstDataSource[row]];
        
    }else if (component == 1){
        [self.valueArrM replaceObjectAtIndex:1 withObject:self.secondDataSource[row]];
        
    }else{
        [self.valueArrM replaceObjectAtIndex:2 withObject:self.thirdDataSource[row]];
    }
    
}




////返回视图
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
//{
//    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
//
//    if (component == 0) {
//
//        label.text = self.m_yearArray[row];
//
//    } else {
//
//        label.text = self.m_monthArray[row];
//    }
//
//
//    return label;
//}


#pragma mark - setter

- (void)setTransValueCallBack:(TransValueCallBack)callBack
{
    _transValueCallBack = callBack;
}

- (void)setFirstDataSource:(NSArray *)firstDataSource
{
    _firstDataSource = firstDataSource;
    [self.valueArrM replaceObjectAtIndex:0 withObject:firstDataSource[0]];
}

- (void)setSecondDataSource:(NSArray *)secondDataSource
{
    _secondDataSource = secondDataSource;
    [self.valueArrM replaceObjectAtIndex:1 withObject:secondDataSource[0]];
}

- (void)setThirdDataSource:(NSArray *)thirdDataSource
{
    _thirdDataSource = thirdDataSource;
    [self.valueArrM replaceObjectAtIndex:2 withObject:thirdDataSource[0]];
}



#pragma mark - 懒加载

- (UIPickerView *)pickerView
{
    if (_pickerView == nil) {
        UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, self.bounds.size.width-80, 200)];
//        pickerView.backgroundColor = ThemColor2;
        pickerView.delegate = self;
        pickerView.dataSource = self;
        [self addSubview:pickerView];
        
        _pickerView = pickerView;
    }
    return _pickerView;
}


- (NSMutableArray *)valueArrM
{
    if (_valueArrM == nil) {
        _valueArrM = [[NSMutableArray alloc]initWithObjects:@"", @"", @"", nil];
    }
    return _valueArrM;
}




@end
